package hhm.android.main;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.TypedValue;

import androidx.core.content.ContextCompat;

import hhm.android.library.calendarView.Calendar;
import hhm.android.library.calendarView.CalendarUtil;
import hhm.android.library.calendarView.MonthView;


/**
 * 高仿魅族日历布局
 * Created by huanghaibin on 2017/11/15.
 */

public class MySimpleMonthView extends MonthView {

    private Paint mainColorFullPaint;
    private Paint mainColorStokePaint;
    private Paint yellowPaint;
    private Paint whitePaint;
    private int mRadius;
    private Bitmap bitmap;
    Rect srcRect;
    RectF dstRecF;

    public MySimpleMonthView(Context context) {
        super(context);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.tooth_brush_star);
        srcRect = new Rect();
        dstRecF = new RectF();

        mainColorFullPaint = new Paint();
        mainColorStokePaint = new Paint();
        yellowPaint = new Paint();
        whitePaint = new Paint();
        mainColorFullPaint.setAntiAlias(true);
        mainColorFullPaint.setStyle(Paint.Style.FILL);
        mainColorFullPaint.setTextAlign(Paint.Align.CENTER);
        mainColorFullPaint.setColor(ContextCompat.getColor(context, R.color.mainColor));

        yellowPaint.setFakeBoldText(true);
        yellowPaint.setAntiAlias(true);
        yellowPaint.setStyle(Paint.Style.FILL);
        yellowPaint.setTextAlign(Paint.Align.CENTER);
        yellowPaint.setColor(Color.YELLOW);

        whitePaint.setFakeBoldText(true);
        whitePaint.setAntiAlias(true);
        whitePaint.setStyle(Paint.Style.FILL);
        whitePaint.setTextAlign(Paint.Align.CENTER);
        whitePaint.setColor(Color.WHITE);
        whitePaint.setFakeBoldText(true);
        whitePaint.setTextSize(dp2px(13f));

        mainColorStokePaint.setAntiAlias(true);
        mainColorStokePaint.setStyle(Paint.Style.STROKE);
        mainColorStokePaint.setTextAlign(Paint.Align.CENTER);
        mainColorStokePaint.setColor(ContextCompat.getColor(context, R.color.mainColor));
        mainColorStokePaint.setStrokeWidth(dp2px(1f));
        //虚线 绘制长度为4的实线后再绘制长度为4的空白区域，0位间隔
        mainColorStokePaint.setPathEffect(new DashPathEffect(new float[]{10, 5}, 0));
    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
        mSchemePaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onLoopStart(int x, int y) {

    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
//        canvas.drawRect(x + mItemWidth / 2 - mRadius, y + mItemHeight / 2 - mRadius, x + mItemWidth / 2 + mRadius, y + mItemHeight / 2 + mRadius / 2 * 3, mSelectedPaint);

//        canvas.drawRoundRect(x + mItemWidth / 2 - mRadius, y + mItemHeight / 2 - mRadius, x + mItemWidth / 2 + mRadius, y + mItemHeight / 2 + mRadius / 5 * 7, dp2px(8f), dp2px(8f), mSelectedPaint);

//        int cx = x + mItemWidth / 2;
//        int cy = y + mItemHeight / 2;
//        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);

        return true;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
//        canvas.drawRect(x + mItemWidth / 2 - mRadius, y + mItemHeight / 2 - mRadius, x + mItemWidth / 2 + mRadius, y + mItemHeight / 2 + mRadius / 2 * 3, mSelectedPaint);

//        int cx = x + mItemWidth / 2;
//        int cy = y + mItemHeight / 2;
//        canvas.drawCircle(cx, cy, mRadius, mSchemePaint);
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = y + mTextBaseLine - mRadius / 5 * 2;
        float cx = x + mItemWidth / 2;

        if (hasScheme) {

            //0：没星，1：只有上午有星，2：只有下午有星，3：满星
            int starStatus = Integer.parseInt(calendar.getScheme());

            if (isSelected) {
                canvas.drawRoundRect(x + mItemWidth / 2 - mRadius, y + mItemHeight / 2 - mRadius, x + mItemWidth / 2 + mRadius, y + mItemHeight / 2 + mRadius / 5 * 7, dp2px(8f), dp2px(8f), mainColorFullPaint);
            } else {
                canvas.drawRoundRect(x + mItemWidth / 2 - mRadius, y + mItemHeight / 2 - mRadius, x + mItemWidth / 2 + mRadius, y + mItemHeight / 2 + mRadius / 5 * 7, dp2px(8f), dp2px(8f), mainColorStokePaint);
            }

            float bitmapSize = mRadius / 5 * 3;
            float b1x = x + mRadius / 5 * 3;
            float b1y = y + mTextBaseLine;

            b1x = x + mItemWidth / 2 - mRadius / 5 * 4;

            //srcRect定义了要绘制Bitmap的哪一部分
            srcRect.left = 0;
            srcRect.right = bitmap.getWidth();
            srcRect.top = 0;
            srcRect.bottom = bitmap.getHeight();

            //dstRecF定义了要将绘制的Bitmap拉伸到哪里
            dstRecF.left = b1x;
            dstRecF.right = b1x + bitmapSize;
            dstRecF.top = b1y;
            dstRecF.bottom = b1y + bitmapSize;

            //早上刷牙的星星
            if (starStatus == 1 || starStatus == 3) {
                canvas.drawBitmap(bitmap, srcRect, dstRecF, yellowPaint);
            }

            float b2x = x + mRadius / 5 * 7;
            float b2y = y + mTextBaseLine;

            b2x = x + mItemWidth / 2 + mRadius / 5 * 4 - bitmapSize;

            //srcRect定义了要绘制Bitmap的哪一部分
            srcRect.left = 0;
            srcRect.right = bitmap.getWidth();
            srcRect.top = 0;
            srcRect.bottom = bitmap.getHeight();

            //dstRecF定义了要将绘制的Bitmap拉伸到哪里
            dstRecF.left = b2x;
            dstRecF.right = b2x + bitmapSize;
            dstRecF.top = b2y;
            dstRecF.bottom = b2y + bitmapSize;

            //晚上刷牙的星星
            if (starStatus == 2 || starStatus == 3) {
                canvas.drawBitmap(bitmap, srcRect, dstRecF, yellowPaint);
            }
        }

        if (isSelected && hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, whitePaint);
        } else if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, mSelectTextPaint);
        } else if (hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, calendar.isCurrentDay() ? mCurDayTextPaint : calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, calendar.isCurrentDay() ? mCurDayTextPaint : calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }

    public int dp2px(Float dpValue) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, getResources().getDisplayMetrics()));
    }
}
